#include "etcd.hpp"
#include "channel.hpp"
#include "utils.hpp"
#include <gflags/gflags.h>
#include <gtest/gtest.h>
#include <thread>
#include "user.pb.h"
#include "base.pb.h"



DEFINE_bool(run_mode, false, "程序的运行模式，false-调试； true-发布；");
DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，用于指定日志输出等级");


DEFINE_string(etcd_host, "http://127.0.0.1:2379", "服务注册中心地址");
DEFINE_string(base_service, "/service", "服务监控根目录");
DEFINE_string(user_service, "/service/user_service", "当前实例名称");


ServiceManager::ptr _user_channels;

lch_im::UserInfo user_info;

std::string login_ssid;
std::string new_nickname = "亲爱的猪妈妈";

TEST(用户子服务测试, 用户注册测试) {
    auto channel = _user_channels->choose(FLAGS_user_service);//获取通信信道
    ASSERT_TRUE(channel);
    user_info.set_nickname("猪爸爸");

    lch_im::UserRegisterReq req;
    req.set_request_id(uuid());
    req.set_nickname(user_info.nickname());
    req.set_password("123456");
    lch_im::UserRegisterRsp rsp;
    brpc::Controller cntl;
    lch_im::UserService_Stub stub(channel.get());
    stub.UserRegister(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}
// TEST(用户子服务测试, 用户登录测试) {
//     auto channel = _user_channels->choose(FLAGS_user_service);//获取通信信道
//     ASSERT_TRUE(channel);

//     lch_im::UserLoginReq req;
//     req.set_request_id(uuid());
//     req.set_nickname("这是一个美丽的猪妈妈");
//     req.set_password("123456");
//     lch_im::UserLoginRsp rsp;
//     brpc::Controller cntl;
//     lch_im::UserService_Stub stub(channel.get());
//     stub.UserLogin(&cntl, &req, &rsp, nullptr);
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(rsp.success());
//     login_ssid = rsp.login_session_id();
// }
//TEST(用户子服务测试, 用户头像设置测试) {
//    auto channel = _user_channels->choose(FLAGS_user_service);//获取通信信道
//    ASSERT_TRUE(channel);
//
//    lch_im::SetUserAvatarReq req;
//    req.set_request_id(uuid());
//    req.set_user_id(user_info.user_id());
//    req.set_session_id(login_ssid);
//    req.set_avatar(user_info.avatar());
//    lch_im::SetUserAvatarRsp rsp;
//    brpc::Controller cntl;
//    lch_im::UserService_Stub stub(channel.get());
//    stub.SetUserAvatar(&cntl, &req, &rsp, nullptr);
//    ASSERT_FALSE(cntl.Failed());
//    ASSERT_TRUE(rsp.success());
//}
//TEST(用户子服务测试, 用户签名设置测试) {
//    auto channel = _user_channels->choose(FLAGS_user_service);//获取通信信道
//    ASSERT_TRUE(channel);
//
//    lch_im::SetUserDescriptionReq req;
//    req.set_request_id(uuid());
//    req.set_user_id(user_info.user_id());
//    req.set_session_id(login_ssid);
//    req.set_description(user_info.description());
//    lch_im::SetUserDescriptionRsp rsp;
//    brpc::Controller cntl;
//    lch_im::UserService_Stub stub(channel.get());
//    stub.SetUserDescription(&cntl, &req, &rsp, nullptr);
//    ASSERT_FALSE(cntl.Failed());
//    ASSERT_TRUE(rsp.success());
//}
//TEST(用户子服务测试, 用户昵称设置测试) {
//    auto channel = _user_channels->choose(FLAGS_user_service);//获取通信信道
//    ASSERT_TRUE(channel);
//
//    lch_im::SetUserNicknameReq req;
//    req.set_request_id(uuid());
//    req.set_user_id(user_info.user_id());
//    req.set_session_id(login_ssid);
//    req.set_nickname(new_nickname);
//    lch_im::SetUserNicknameRsp rsp;
//    brpc::Controller cntl;
//    lch_im::UserService_Stub stub(channel.get());
//    stub.SetUserNickname(&cntl, &req, &rsp, nullptr);
//    ASSERT_FALSE(cntl.Failed());
//    ASSERT_TRUE(rsp.success());
//}


// TEST(用户子服务测试, 用户信息获取测试) {
//     auto channel = _user_channels->choose(FLAGS_user_service);//获取通信信道
//     ASSERT_TRUE(channel);

//     lch_im::GetUserInfoReq req;
//     req.set_request_id(uuid());
//     req.set_user_id(user_info.user_id());
//     req.set_session_id(login_ssid);
//     lch_im::GetUserInfoRsp rsp;
//     brpc::Controller cntl;
//     lch_im::UserService_Stub stub(channel.get());
//     stub.GetUserInfo(&cntl, &req, &rsp, nullptr);
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(rsp.success());

// }

// void set_user_avatar(const std::string &uid, const std::string &avatar) {
//     auto channel = _user_channels->choose(FLAGS_user_service);//获取通信信道
//     ASSERT_TRUE(channel);
//     lch_im::SetUserAvatarReq req;
//     req.set_request_id(uuid());
//     req.set_user_id(uid);
//     req.set_session_id(login_ssid);
//     req.set_avatar(avatar);
//     lch_im::SetUserAvatarRsp rsp;
//     brpc::Controller cntl;
//     lch_im::UserService_Stub stub(channel.get());
//     stub.SetUserAvatar(&cntl, &req, &rsp, nullptr);
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(rsp.success());
// }

// TEST(用户子服务测试, 批量用户信息获取测试) {
//     set_user_avatar("用户ID1", "小猪佩奇的头像数据");
//     set_user_avatar("用户ID2", "小猪乔治的头像数据");
//     auto channel = _user_channels->choose(FLAGS_user_service);//获取通信信道
//     ASSERT_TRUE(channel);

//     lch_im::GetMultiUserInfoReq req;
//     req.set_request_id(uuid());
//     req.add_users_id("用户ID1");
//     req.add_users_id("用户ID2");
//     req.add_users_id("afb0-40d41359-0000");
//     lch_im::GetMultiUserInfoRsp rsp;
//     brpc::Controller cntl;
//     lch_im::UserService_Stub stub(channel.get());
//     stub.GetMultiUserInfo(&cntl, &req, &rsp, nullptr);
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(rsp.success());
//     auto users_map = rsp.mutable_users_info();
//     lch_im::UserInfo fuser = (*users_map)["afb0-40d41359-0000"];
//     ASSERT_EQ(fuser.user_id(), "afb0-40d41359-0000");
//     ASSERT_EQ(fuser.nickname(), "猪爸爸");
//     ASSERT_EQ(fuser.description(), "");
//     ASSERT_EQ(fuser.phone(), "");
//     ASSERT_EQ(fuser.avatar(), "");

//     lch_im::UserInfo puser = (*users_map)["用户ID1"];
//     ASSERT_EQ(puser.user_id(), "用户ID1");
//     ASSERT_EQ(puser.nickname(), "小猪佩奇");
//     ASSERT_EQ(puser.description(), "这是一只小猪");
//     ASSERT_EQ(puser.phone(), "手机号1");
//     ASSERT_EQ(puser.avatar(), "小猪佩奇的头像数据");
    
//     lch_im::UserInfo quser = (*users_map)["用户ID2"];
//     ASSERT_EQ(quser.user_id(), "用户ID2");
//     ASSERT_EQ(quser.nickname(), "小猪乔治");
//     ASSERT_EQ(quser.description(), "这是一只小小猪");
//     ASSERT_EQ(quser.phone(), "手机号2");
//     ASSERT_EQ(quser.avatar(), "小猪乔治的头像数据");
// }

int main(int argc, char *argv[])
{
    google::ParseCommandLineFlags(&argc, &argv, true);
    init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    //1. 先构造Rpc信道管理对象
    _user_channels = std::make_shared<ServiceManager>();
    _user_channels->declared(FLAGS_user_service);
    auto put_cb = std::bind(&ServiceManager::onServiceOnline, _user_channels.get(), std::placeholders::_1, std::placeholders::_2);
    auto del_cb = std::bind(&ServiceManager::onServiceOffline, _user_channels.get(), std::placeholders::_1, std::placeholders::_2);
    
    //2. 构造服务发现对象
    Discovery::ptr dclient = std::make_shared<Discovery>(FLAGS_etcd_host, FLAGS_base_service, put_cb, del_cb);
    
    
    testing::InitGoogleTest(&argc, argv);
    LOG_DEBUG("开始测试！");
    return RUN_ALL_TESTS();
}
